
#include "heartbeat.h"

#define INFO_LENGTH 2*MAX_LINE_LENGTH+MAX_HOSTNAME_LENGTH+64+3*sizeof(int)+2*sizeof(unsigned long)

Err  rp_rstatd_hearbeat()
{
	char fname[] = "rp_rstatd_heartbeat";
	char msg[] = "fatal error";
	Err err;
	void *buf;
	int size;
	FLOG_IN(fname);
	
	if (!global.sb_live.total)
	{
		rp_log(LOG_DEBUG, fname, "No live rpointd, nothing to report");
		return E_OK;
	}

	/*	collect info    */
	if (rp_rstatd_info(&global.info) != E_OK)
	{
		rp_log(LOG_WARNING, fname, "Failed to collect te host information");
		return E_ERROR;
	}
	
	/*	send the info to rindexd	   */
	size = INFO_LENGTH;
	size += (global.info.fv->size-global.info.fv->free)*sizeof(File);
	size += global.info.sb_info.total*(sizeof(Worker)-2*sizeof(Worker*));
	buf = (void*)malloc(size); 

	memcpy(buf, &global.info, INFO_LENGTH);
	memcpy(buf+INFO_LENGTH, global.info.fv->files, (global.info.fv->size-global.info.fv->free)*sizeof(File));
	memcpy(buf+INFO_LENGTH+(global.info.fv->size-global.info.fv->free)*sizeof(File), global.info.sb_info);
		
	rp_comm_send(global.fd_rindexd, &size, sizeof(int));
	if (rp_comm_send(global.fd_rindexd, buf, size) != E_OK)
	{
		rp_log(LOG_WARNING, fname, "Cannot to send the local information to rindexd");
		return E_ERROR;
	}
	free (buf);
	
	FLOG_OUT(fname);
	return E_OK;
}


